package cn.zhl.设计模式.原型.stream;

import cn.zhl.设计模式.原型.DeepObj;

import java.io.*;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2021-03-07 14:29
 **/
public class CloneStreamDemo implements Serializable {
	private String name;
	private int age;
	private DeepObj deepObj;

	public CloneStreamDemo myClone(){
		ByteArrayOutputStream bos = null;
		ObjectOutputStream oos = null;

		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;

		try {
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(this);

			bis = new ByteArrayInputStream(bos.toByteArray());
			ois = new ObjectInputStream(bis);
			return (CloneStreamDemo) ois.readObject();
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			if(ois != null){
				try {
					ois.close();
				}catch (Exception e){
					e.printStackTrace();
				}
			}
			if(bis != null){
				try {
					bis.close();
				}catch (Exception e){
					e.printStackTrace();
				}
			}
			if(oos != null){
				try {
					oos.close();
				}catch (Exception e){
					e.printStackTrace();
				}
			}
			if(bos != null){
				try {
					bos.close();
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public DeepObj getDeepObj() {
		return deepObj;
	}

	public void setDeepObj(DeepObj deepObj) {
		this.deepObj = deepObj;
	}
}
